import numpy as np
import pandas as pd
from pandas.api.types import is_numeric_dtype
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import optimizers
from sim.nnutil import loadModel

def loadData():
    data=pd.read_csv("胜六注水-异常数据/二号泵.csv",index_col=0)
    data = data.dropna(axis=1, how='all')
    fields = list(data.columns)
    ls = []
    for field in fields:
        if is_numeric_dtype(data[field]) and \
                "累积" not in field and \
                "累计" not in field and \
                field != "label":
            ls.append(field)
    remove=['运行状态','日耗电量']
    for item in remove:
        ls.remove(item)
    data=data[ls]
    data['异常代码'].fillna(-1,inplace=True)#文件中异常代码存在空缺值，乙方说空缺值表示这条数据正常，使用-1替换
    data['异常代码']+=1#所有的异常代码加1
    #原本0表示参数缺失（必要的参数都为nan），经过上一步后1表示参数缺失，0表示正常
    data.fillna(0,inplace=True)#剩下的所有nan值替换为0
    ls.remove('异常代码')
    import json
    fi = open('ran.json', 'r', encoding='utf-8')
    dic = json.load(fi)
    fi.close()
    for field in ls:
        # min = dic[field]['min']
        # max = dic[field]['max']
        min = data[field].min()
        max = data[field].max()
        ran = max - min
        data[field] = data[field].map(lambda x: (x - min) / ran)
    data=data.values
    data,label=data[:,0:-1],data[:,-1]
    label=label.astype('int8')
    #保证每一个分类的数目一致，找出数目最少的那一个分裂
    labelIndiceGroup=[]
    cateNum=6
    minNum=2**30
    for i in range(cateNum):
        indice=np.where(label==i)[0]
        labelIndiceGroup.append(indice)
        if len(indice)<minNum and len(indice)>0:
            minNum=len(indice)
    labelIndice=np.array([],dtype='int32')
    for i in range(cateNum):
        labelIndice=np.concatenate([labelIndice,labelIndiceGroup[i][:minNum]])
    np.random.shuffle(labelIndice)
    data=data[labelIndice]
    label=label[labelIndice]
    return data,label

if __name__=="__main__":
    data,label=loadData()
    cateNum=6
    label = keras.utils.to_categorical(label, num_classes=cateNum)
    model = loadModel('./modelANN.tf')
    score = model.evaluate(data, label, verbose=0)
    print("Test loss:", score[0])
    print("Test accuracy:", score[1])